<template>
  <div>
    <el-dialog v-model="dialogVisible" title="项目编辑器" width="1000px"  draggable :fullscreen="false" :show-close="false">
      <div class="table-container">
        <!-- 表格 -->
        <el-table :data="projects" style="width: 100%">
          <el-table-column label="序号" type="index" :width="50"></el-table-column>
          <el-table-column label="名称">
            <template #default="scope">
              <el-input
                v-if="scope.$index === 0"
                v-model="newProject.name"
                placeholder="请输入名称"
                @keyup.enter="addProject"
              ></el-input>
              <span v-else>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="URL" width="500px">
            <template #default="scope">
              <el-input
                v-if="scope.$index === 0"
                v-model="newProject.url"
                placeholder="请输入 URL"
                @keyup.enter="addProject"
              ></el-input>
              <span v-else @click="copyText(scope.row.url)" class="clickable-url">{{ scope.row.url }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100">
            <template #default="scope">
              <el-button
                v-if="scope.$index !== 0"
                type="danger"
                icon="el-icon-delete"
                circle
                @click="deleteProject(scope.$index)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';

const dialogVisible = ref(true);
const projects = ref(JSON.parse(localStorage.getItem('.project')) || []);
const newProject = ref({ name: '', url: '' });
const maxProjects = 500;

const addProject = () => {
  if (newProject.value.name && newProject.value.url) {
    if (projects.value.length < maxProjects) {
      projects.value.push({ ...newProject.value });
      newProject.value = { name: '', url: '' }; // 清空输入框
      updateLocalStorage();
    } else {
      ElMessage.error('最多只能保存 500 个项目');
    }
  } else {
    ElMessage.error('名称和 URL 都不能为空');
  }
};

const deleteProject = (index) => {
  if (projects.value.length > 1 && index < projects.value.length) {
    projects.value.splice(index, 1);
    updateLocalStorage();
    ElMessage.success('项目已删除');
  }
};

const copyText = (text) => {
  navigator.clipboard.writeText(text);
  ElMessage.success('URL 已复制到剪贴板');
  // 这里进行判断后进行打开 
  
};

const updateLocalStorage = () => {
  localStorage.setItem('.project', JSON.stringify(projects.value));
};

onMounted(() => {
  if (projects.value.length === 0) {
    projects.value.push({ name: '', url: '' }); // 添加一个初始空项目
  }
});
</script>

<style scoped>
.table-container {
  max-height: 400px;
  overflow-y: auto;
}

.clickable-url {
  cursor: pointer;
  color: #409eff;
  text-decoration: underline;
}
</style>